package com.example.core.http

import com.google.gson.Gson
import okhttp3.Call
import okhttp3.Callback
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import java.io.IOException
import java.lang.reflect.Type
import java.security.SecureRandom
import javax.net.ssl.HostnameVerifier
import javax.net.ssl.SSLContext
import javax.net.ssl.SSLSession
import javax.net.ssl.TrustManager
import javax.net.ssl.X509TrustManager
import javax.security.cert.X509Certificate

class HttpClient {


    fun <T> get(path: String, type: Type, callback: EntityCallback<T>) {
        val request = Request.Builder()
            .url("https://api.hencoder.com/$path")
            .build()
        okHttpClient.newCall(request).enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                e.printStackTrace()
                callback.onFailure("网络异常！")
            }

            override fun onResponse(call: Call, response: Response) {
                when (response.code()) {
                    in 200..299 -> {
                        val body = response.body()
                        var json: String? = null
                        try {
                            json = body!!.string()
                        } catch (e: IOException) {
                            e.printStackTrace()
                        }
                        callback.onSuccess(convert(json!!, type))
                    }

                    in 400..499 -> {
                        callback.onFailure("客户端错误")
                    }

                    in 501..599 -> {
                        callback.onFailure("服务器错误")
                    }

                    else -> {
                        callback.onFailure("未知错误")
                    }
                }
            }

        })

    }

    companion object {

        val INSTANCE: HttpClient by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
            HttpClient()
        }

        private val gson = Gson()

        private fun <T> convert(json: String, type: Type): T = gson.fromJson(json, type)

        private val hostnameVerifier = HostnameVerifier { _, _ -> true }

        private val IGNORE_SSL_TRUST_MANAGER_X509 = object : X509TrustManager {
            override fun checkClientTrusted(
                p0: Array<out java.security.cert.X509Certificate>?,
                p1: String?
            ) {

            }

            override fun checkServerTrusted(
                p0: Array<out java.security.cert.X509Certificate>?,
                p1: String?
            ) {
            }

            override fun getAcceptedIssuers(): Array<java.security.cert.X509Certificate> = arrayOf()
        }
        private val sslContext = SSLContext.getInstance("SSL").apply {
            init(null, arrayOf(IGNORE_SSL_TRUST_MANAGER_X509), SecureRandom())
        }

        val okHttpClient = OkHttpClient.Builder()
            .sslSocketFactory(sslContext.socketFactory, IGNORE_SSL_TRUST_MANAGER_X509)
            .hostnameVerifier(hostnameVerifier)
            .build()

    }
}